<?php
	class CountModel extends Model {
		/**
		 * 获得年级，班级等的基本信息相关的人数
		 * @param Array $where
		 */
		public function getNum($where){
			$getCount=M("Baseinfo");
			if (empty($where)) {
				return $getCount->count();
			}
			return $getCount->where($where)->count();
		}
		/**
		 * 获得一个班级的补考详细信息
		 * @param int $majorcode
		 * @param int $classCode
		 * @param int $year
		 */
		public function getClassBkinfo($gradeCode,$majorCode,$classCode,$year,$mode="up"){
			$getCount=M("Sanctioninfo");
			$getSID=M("Baseinfo");
			$where["grade"]=$gradeCode;
			$where["major"]=$majorCode;
			$where["class"]=$classCode;
			$sidList=$getSID->where($where)->field("sid")->select();//获得相应班级的学号列表
			$count=count($sidList);//获取列表数量
			unset($where["grade"]);
			unset($where["major"]);
			unset($where["class"]);
			$where["year"]=$year;
			for ($i=0;$i<$count;$i++){
					//$ms=$getCount->where($where)->getField("bkms");
					$where["sid"]=$sidList[$i]['sid'];
					$info=$getCount->where($where)->field("bkms,cxms")->select();
					if($info[0]["bkms"]=="0&0"&&$info[0]["cxms"]=="0&0"){
						continue;
					}
					if($info[0]["bkms"]==""&&$info[0]["cxms"]==""){
						continue;
					}
					$bklist[$i]["sid"]=$sidList[$i]['sid'];
					$bklist[$i]["name"]=$getSID->where("sid=".$sidList[$i]['sid'])->getField("name");
					$bklist[$i]["year"]=$getCount->where($where)->getField("year");
					if ($mode=="up") {//上半学期
						$tmp=explode("&", $info[0]["bkms"]);
						if((int)$tmp[0]==0){
							unset($bklist[$i]);
							continue;
						}
						$bklist[$i]["bkms"]=(int)$tmp[0];//补考
						$tmp1=explode("&", $info[0]["cxms"]);
						if((int)$tmp1[0]==0){
							unset($bklist[$i]);
							continue;
						}
						$bklist[$i]["cxms"]=(int)$tmp1[0];//重修
					}else{//下班学期
						$tmp=explode("&", $info[0]["bkms"]);
						if((int)$tmp[1]==0){
							unset($bklist[$i]);
							continue;
						}
						$bklist[$i]["bkms"]=(int)$tmp[1];//补考
						$tmp1=explode("&", $info[0]["cxms"]);
						if((int)$tmp1[1]==0){
							unset($bklist[$i]);
							continue;
						}
						$bklist[$i]["cxms"]=(int)$tmp1[1];//重修
					}
				}
				return $bklist;
		}
		/**
		 * 该函数用于获取一个字段中不重复的值
		 * @param string $field
		 * @param string $table
		 */
		public function getNoRepeat($field,$table,$where){
			$table=ucfirst($table);
			$deal=M($table);
			return $deal->where($where)->Distinct(true)->field($field)->select();
		}
		/**
		 * 获得年级 全系党员人数
		 * @param $type 年级(grade) 全系（all）
		 * @param $code 相关的代号
		 */
		public function getPartyInfo($type,$code){
			$party=M("Baseinfo");
			$where["isparty"]=1;//筛选出事党员的条件
			switch ($type){
				case "grade"://获得各年级党员人数
					$where["grade"]=$code;
					$count=$party->where($where)->count();
					break;
				case "all"://获得全系党员的人数
					$count=$party->where($where)->count();
					break;
			}
			return $count;
		}
		//获得班级党员人数
		public function getClassPartyInfo($grade,$major,$class,$mode="num"){
			$party=M("Baseinfo");
			$where["isparty"]=1;
			$where["grade"]=(int)$grade;
			$where["major"]=(int)$major;
			$where["class"]=(int)$class;
			if ($mode=="num") {
				return $party->where($where)->count();
			}else{
				return $party->where($where)->field("sid,name,sex")->select();
			}
			
		}
		/**
		 * 获得个年级 专业 班级的符修情况
		 * @param string $type grade major class
		 * @param int $code
		 * @param Array $remark ["grade"],["major"] 当type为class时有效
		 * @param string $mode "num"返回数量 "list"返回学会学生列表
		 */
		public function getSecondMajor($type,$code,$remark,$mode="num"){
			$s=M("Baseinfo");
			$where["secondmajor"]=array("neq","");//筛选出已经辅修的条件
			switch ($type){
				case "grade":
					$where["grade"]=$code;
					if ($mode=="num") {
						return $s->where($where)->count();
					}else{
						return $s->where($where)->field("sid,name,sex")->select();
					}
					
					break;
				case "major":
					$where["major"]=$code;
					if ($mode=="num") {
						return $s->where($where)->count();
					}else{
						return $s->where($where)->field("sid,name,sex")->select();
					}
					
					break;
				case "class":
					$where["grade"]=$remark["grade"];
					$where["major"]=$remark["major"];
					$where["class"]=$code;
					if ($mode=="num") {
						return $s->where($where)->count();
					}else{
						return $s->where($where)->field("sid,name,sex")->select();
					}
					
					break;
				default:
					return false;
			}
		}
		/**
		 * 检查学生是否或的某项奖励或处罚
		 * @param string $sid
		 * @param string $type
		 * @param int $year
		 * @return boolean
		 */
		public function checkSanctioBySID($sid,$type,$year){
			$jf=M("Sanctioninfo");
			$where["sid"]=$sid;
			$where[$type]=array("gt",0);
			$where[year]=$year;
			$count=$jf->where($where)->count();
			if ($count>0) {
				return true;
		
			}else{
				return false;
			}
		}
		
		/*
		 * 获得班级 年级 全系的申请入党 党校 预备党员 正式党员的人数
		 * @param $type
		 * @param $code 如果type为class 则code为数组
		 * @param $mode sqrd dxrs ybdy zsdy
		 */
		public function getPartyNum($type,$code,$mode){
			$st=M("Baseinfo");
			$party=M("Partyinfo");
			if ($type=="class") {
				$where["grade"]=$code["grade"];
				$where["major"]=$code["major"];
				$where["class"]=$code["class"];
			}else{
				$where[$type]=$code;
			}
			switch ($mode){
				case "sqrd"://申请入党 即时间小于当前时间
					$field="sqrdsj";//申请入党时间
					break;
				case "dxrs"://党校人数
					$field="dxkssj";//党校开始时间
					break;
				case "ybdy"://预备党员
					$field="ybdypzsj";//预备党员批准时间
					break;
				case "zsdy"://正式党员
					$field="zzsj";//转正时间
					break;
				default:
					return false;
			}
			$st_list=$st->where($where)->field("sid")->select();
			$count=count($st_list);
			echo "count=".$count."<br>";
			$today=date("Y-m-d",time());
			echo "today=".$today."<br>";
			$num=0;
			for ($i=0;$i<$count;$i++){
				echo $st_list[$i]["sid"]."<br>";
				$time=$party->where("sid=".$st_list[$i]["sid"])->getField($field);
				if (empty($time)) {
					continue;
				}
				if ($time<=$today) {//时间小于或等于今天则成立
					$num++;
				}
			}
			echo $num;
			return $num;
		}
		
		/**
		 * 统计个年级 班级  专业的信息
		 */
		public function getCountInfo($where){
			$result["allNum"]=$this->getNum($where);//统计总人数
			$where["sex"]="男";
			$result["sexB"]=$this->getNum($where);//统计男生人数
			$result["sexB-bl"]=(round((int)$result["sexB"]/(int)$result["allNum"],2)*100)."%";
			$where["sex"]="女";
			$result["sexG"]=$this->getNum($where);//统计女生人数
			$result["sexG-bl"]=(round((int)$result["sexG"]/(int)$result["allNum"],2)*100)."%";
			unset($where["sex"]);
			$where["isparty"]=1;
			$result["party"]=$this->getNum($where);//当员人数
			$result["party-bl"]=(round((int)$result["party"]/(int)$result["allNum"],2)*100)."%";
			$where["isparty"]=2;
			$result["ty"]=$this->getNum($where);
			$result["ty-bl"]=(round((int)$result["ty"]/(int)$result["allNum"],2)*100)."%";
			unset($where["isparty"]);
			$nationList=$this->getNoRepeat("nation","baseinfo",$where);
			$nationCount=count($nationList);
			for ($i=0;$i<$nationCount;$i++){
				$where["nation"]=$nationList[$i]["nation"];
				if($this->getNum($where)==0){
					continue;
				}
				$result["nation"][$i]["nation"]=$where["nation"];
				$result["nation"][$i]["num"]=$this->getNum($where);
				$result["nation"][$i]["bl"]=(sprintf("%.2f", $this->getNum($where)/$result["allNum"])*100)."%";
				
			}
			unset($where["nation"]);
			$comeList=$this->getNoRepeat("comefrom", "baseinfo",$where);
			$comeCount=count($comeList);
			for ($i=0;$i<$comeCount;$i++){
				$where["comefrom"]=$comeList[$i]["comefrom"];
				if($this->getNum($where)==0){
					continue;
				}
				$result["comefrom"][$i]["comefrom"]=$where["comefrom"];
				$result["comefrom"][$i]["num"]=$this->getNum($where);
				$result["comefrom"][$i]["bl"]=(sprintf("%.2f", $this->getNum($where)/$result["allNum"])*100)."%";
				
			}
			unset($where["comefrom"]);
			$where["secondmajor"]=array("neq","");//获取辅修字段部位一个空格的条件
			$result["secondMajorNum"]=$this->getNum($where);
			$result["secondMajor-bl"]=(round((int)$result["secondMajorNum"]/(int)$result["allNum"],2)*100)."%";
			$secondList=$this->getNoRepeat("secondmajor","baseinfo",$where);
			for ($i=0;$i<$result["secondMajorNum"];$i++){
				$where["secondmajor"]=$secondList[$i]["secondmajor"];
				if($this->getNum($where)==0){
					continue;
				}
				$result["secondmajor"][$i]["secondmajor"]=$where["secondmajor"];
				$result["secondmajor"][$i]["num"]=$this->getNum($where);
				$result["secondmajor"][$i]["bl"]=(sprintf("%.2f", $this->getNum($where)/$result["secondMajorNum"])*100)."%";
			}
			unset($where["secondmajor"]);
			return $result;
		}
		
		public function checkPartyType($sid,$type){
			$party=M("Partyinfo");
			$tmp=$party->where("sid=".$sid)->select();
			if(empty($tmp)){
				return false;
			}else{
				$one=$tmp[0];
			}
			$today=date("Y-m-d");
			switch($type){
				case "sqrd":
					if($one["sqrdsj"]!="0000-00-00" && $one["sqrdsj"]<=$today){
						if($one[dxkssj]=="0000-00-00"){
							return true;
						}else{
							return false;
						}
						
						
					}else{
						return false;
					}
					break;
				case "dxzd":
					if($one["dxkssj"]!="0000-00-00" && $one["dxkssj"]<=$today && $one["dxbysj"]=="0000-00-00"){
						return true;
					}else{
						return false;
					}
					break;
				case "ybdy":
					if($one["ybdypzsj"]!="0000-00-00" && $one["ybdypzsj"]<=$today){
						if($one["zzsj"]=="0000-00-00"){
							return true;
						}else{
							return false;
						}
					}else{
						return false;
					}
					break;
				case "zsdy":
					if($one["zzsj"]!="0000-00-00" && $one["zzsj"]<=$today){
						return true;
					}else{
						return false;
					}
					break;
				default:
					return false;;
			}
		}
		
	}
 	

?>